package top.continew.admin.sport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;

import org.jetbrains.annotations.Nullable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import top.continew.admin.sport.model.resp.ProjectEntryExcel;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;
import top.continew.admin.sport.mapper.ProjectEntryMapper;
import top.continew.admin.sport.model.entity.ProjectEntryDO;
import top.continew.admin.sport.model.query.ProjectEntryQuery;
import top.continew.admin.sport.model.req.ProjectEntryReq;
import top.continew.admin.sport.model.resp.ProjectEntryDetailResp;
import top.continew.admin.sport.model.resp.ProjectEntryResp;
import top.continew.admin.sport.service.ProjectEntryService;

import java.util.List;

/**
 * 报名业务实现
 *
 * @author xiahaike
 * @since 2024/09/15 02:01
 */
@Service
@RequiredArgsConstructor
public class ProjectEntryServiceImpl extends BaseServiceImpl<ProjectEntryMapper, ProjectEntryDO, ProjectEntryResp, ProjectEntryDetailResp, ProjectEntryQuery, ProjectEntryReq> implements ProjectEntryService {

    @Resource
    private ProjectEntryMapper projectEntryMapper;


    @Override
    public ProjectEntryResp getOneByUserId(ProjectEntryReq projectEntryReq) {
        QueryWrapper<ProjectEntryDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", projectEntryReq.getUserId());
        return getProjectEntryResp(projectEntryReq, queryWrapper);
    }

    @Override
    public ProjectEntryResp getOneByTeamId(ProjectEntryReq projectEntryReq) {
        QueryWrapper<ProjectEntryDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_id", projectEntryReq.getTeamId());
        return getProjectEntryResp(projectEntryReq, queryWrapper);
    }

    @Nullable
    private ProjectEntryResp getProjectEntryResp(ProjectEntryReq projectEntryReq, QueryWrapper<ProjectEntryDO> queryWrapper) {
        queryWrapper.eq("project_id", projectEntryReq.getProjectId());
        ProjectEntryDO projectEntryDO = projectEntryMapper.selectOne(queryWrapper);
        if (projectEntryDO == null) {
            return null;
        }
        ProjectEntryResp projectEntryResp = new ProjectEntryResp();
        BeanUtil.copyProperties(projectEntryDO, projectEntryResp);
        return projectEntryResp;
    }


    @Override
    public PageResp<ProjectEntryResp> myPage(ProjectEntryQuery projectEntryQuery, PageQuery pageQuery) {
        QueryWrapper<ProjectEntryDO> queryWrapper = new QueryWrapper<>();
        IPage<ProjectEntryResp> page = projectEntryMapper.myPage(pageQuery.toPage(), queryWrapper, projectEntryQuery.getProjectId());
        return PageResp.build(page);
    }

    @Override
    public List<ProjectEntryExcel> myList(ProjectEntryQuery query, SortQuery sortQuery) {
        QueryWrapper<ProjectEntryDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", query.getProjectId());
        return projectEntryMapper.myList(queryWrapper, query.getProjectId());
    }
}